package com.cwz.security.handler;

import com.cwz.core.constant.HttpStatus;
import com.cwz.core.exception.auth.NotLoginException;
import com.cwz.core.exception.auth.NotPermissionException;
import com.cwz.core.exception.auth.NotRoleException;
import com.cwz.core.exception.datasource.DataSourceException;
import com.cwz.core.exception.inner.InnerAuthException;
import com.cwz.core.exception.service.ServiceException;
import com.cwz.core.result.R;
import com.cwz.core.utils.string.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: w-demo
 * @description: 全局异常处理器
 * @author: Wen
 **/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
	/**
	 * 权限码异常
	 */
	@ExceptionHandler(NotPermissionException.class)
	public R<Void> handleNotPermissionException(NotPermissionException e, HttpServletRequest request) {
		String requestURI = request.getRequestURI();
		log.error("请求地址'{}',权限码校验失败'{}'", requestURI, e.getMessage());
		return R.fail(HttpStatus.FORBIDDEN, "没有访问权限，请联系管理员授权");
	}

	/**
	 * 角色权限异常
	 */
	@ExceptionHandler(NotRoleException.class)
	public R<Void> handleNotRoleException(NotRoleException e, HttpServletRequest request) {
		String requestURI = request.getRequestURI();
		log.error("请求地址'{}',角色权限校验失败'{}'", requestURI, e.getMessage());
		return R.fail(HttpStatus.FORBIDDEN, "没有访问权限，请联系管理员授权");
	}

	/**
	 * 角色权限异常
	 */
	@ExceptionHandler(NotLoginException.class)
	public R<Void> handleNotLoginException(NotLoginException e, HttpServletRequest request) {
		String requestURI = request.getRequestURI();
		log.error("请求地址'{}',token 校验失败'{}'", requestURI, e.getMessage());
		return R.fail(HttpStatus.FORBIDDEN, "没有访问权限，请联系管理员授权");
	}


	/**
	 * 请求方式不支持
	 */
	@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
	public R<Void> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, HttpServletRequest request) {
		String requestURI = request.getRequestURI();
		log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod());
		return R.fail(e.getMessage());
	}

	/**
	 * 业务异常
	 */
	@ExceptionHandler(ServiceException.class)
	public R<Void> handleServiceException(ServiceException e) {
		log.error(e.getMessage(), e);
		Integer code = e.getCode();
		return StringUtils.isNotNull(code) ? R.fail(code.intValue(), e.getMessage()) : R.fail(e.getMessage());
	}

	/**
	 * 拦截未知的运行时异常
	 */
	@ExceptionHandler(RuntimeException.class)
	public R<Void> handleRuntimeException(RuntimeException e, HttpServletRequest request) {
		String requestURI = request.getRequestURI();
		log.error("请求地址'{}',发生未知异常.", requestURI, e);
		return R.fail(e.getMessage());
	}

	/**
	 * 系统异常
	 */
	@ExceptionHandler(Exception.class)
	public R<Void> handleException(Exception e, HttpServletRequest request) {
		String requestURI = request.getRequestURI();
		log.error("请求地址'{}',发生系统异常.", requestURI, e);
		return R.fail(e.getMessage());
	}

	/**
	 * 自定义 validation 校验异常（绑定异常）
	 */
	@ExceptionHandler(BindException.class)
	public R<Void> handleBindException(BindException e) {
		log.error(e.getMessage(), e);
		String message = e.getAllErrors().get(0).getDefaultMessage();
		return R.fail(message);
	}

	/**
	 * 自定义 validation 校验异常（字段校验失败）
	 */
	@ExceptionHandler(MethodArgumentNotValidException.class)
	public R<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
		log.error(e.getMessage(), e);
		String message = e.getBindingResult().getFieldError().getDefaultMessage();
		return R.fail(message);
	}

	/**
	 * 内部认证异常
	 */
	@ExceptionHandler(InnerAuthException.class)
	public R<Void> handleInnerAuthException(InnerAuthException e) {
		return R.fail(e.getMessage());
	}

	/**
	 * 数据库错误异常类
	 */
	@ExceptionHandler(DataSourceException.class)
	public R<Void> handleDataSourceException(DataSourceException e) {
		return R.fail(e.getMessage());
	}
}
